package com.fenghuajueli.module_host.utils

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.LogUtils
import com.fenghuajueli.libbasecoreui.baseswitch.GeneratorUtil
import com.fenghuajueli.libbasecoreui.baseswitch.SwitchKeyUtils
import com.fenghuajueli.libbasecoreui.constants.AppConfigInfo
import com.fenghuajueli.libbasecoreui.listener.BaseCallBackListener
import com.fenghuajueli.module_user.network.repository.TemplateRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.launch

object LauncherExtUtil {
    private val timestamp: Long by lazy { System.currentTimeMillis() }
    private val nonce: String by lazy { GeneratorUtil.randomSequence(32) }
    private val appId: String by lazy { AppConfigInfo.APPLICATION_ID }
    private val channel: String by lazy { AppConfigInfo.CHANNEL }

    fun v4TopOnKeyFlow() = callbackFlow<Boolean> {
        val startTime = System.currentTimeMillis()
        LogUtils.d("v4TopOnKey请求开始")
        SwitchKeyUtils.getV4TopOnKey(
            timestamp,
            nonce,
            appId,
            object : BaseCallBackListener<String> {
                override fun onSuccess(data: String) {
                    LogUtils.d("v4TopOnKey请求结束  成功：true  耗时：${System.currentTimeMillis() - startTime}")
                    trySend(true)
                }

                override fun onFailed(error: String) {
                    LogUtils.d("v4TopOnKey请求结束  成功：false  耗时：${System.currentTimeMillis() - startTime}  $error")
                    trySend(false)
                }

                override fun onHandlerStart() = Unit
            })

        awaitClose {
            LogUtils.d("v4TopOnKeyFlow: 获取v4TopOnKey流关闭")
        }
    }

    fun v4SwitchKeyFlow() = callbackFlow<Boolean> {
        val startTime = System.currentTimeMillis()
        LogUtils.d("v4开关请求开始")
        SwitchKeyUtils.getV4SwitchKey(
            AppConfigInfo.VERSION_CODE,
            this@LauncherExtUtil.timestamp,
            this@LauncherExtUtil.nonce,
            this@LauncherExtUtil.appId,
            this@LauncherExtUtil.channel,
            object : BaseCallBackListener<String> {
                override fun onSuccess(data: String) {
                    LogUtils.d("v4开关请求结束  成功：true  耗时：${System.currentTimeMillis() - startTime}")
                    trySend(true)
                }

                override fun onFailed(error: String?) {
                    LogUtils.d("v4开关请求结束  成功：false  耗时：${System.currentTimeMillis() - startTime}    $error")
                    trySend(false)
                }

                override fun onHandlerStart() = Unit
            }
        )

        awaitClose {
            LogUtils.d("v4SwitchKeyFlow: 获取v4开关流关闭")
        }
    }

    fun v3SwitchKeyFlow() = callbackFlow<Boolean> {
        val startTime = System.currentTimeMillis()
        LogUtils.d("v3开关请求开始")
        SwitchKeyUtils.getV3SwitchKey(
            AppConfigInfo.VERSION_CODE,
            this@LauncherExtUtil.timestamp,
            this@LauncherExtUtil.nonce,
            this@LauncherExtUtil.appId,
            this@LauncherExtUtil.channel,
            object : BaseCallBackListener<String> {
                override fun onSuccess(data: String) {
                    LogUtils.d("v3开关请求结束  成功：true  耗时：${System.currentTimeMillis() - startTime}")
                    trySend(true)
                }

                override fun onFailed(error: String) {
                    LogUtils.d("v3开关请求结束  成功：false  耗时：${System.currentTimeMillis() - startTime}    $error")
                    trySend(false)
                }

                override fun onHandlerStart() = Unit
            }
        )

        awaitClose {
            LogUtils.d("v3SwitchKeyFlow: 获取v3开关流关闭")
        }
    }

    fun v2SwitchKeyFlow() = callbackFlow<Boolean> {
        val startTime = System.currentTimeMillis()
        LogUtils.d("v2开关请求开始")
        //获取v2广告及其他配置。广告配置和v3相互独立，初始化广告是在v3中。
        SwitchKeyUtils.getSwitchKey(
            this@LauncherExtUtil.timestamp,
            this@LauncherExtUtil.nonce,
            this@LauncherExtUtil.appId,
            this@LauncherExtUtil.channel,
            object : BaseCallBackListener<String> {
                override fun onSuccess(data: String) {
                    LogUtils.d("v2开关请求结束  成功：true  耗时：${System.currentTimeMillis() - startTime}")
                    trySend(true)
                }

                override fun onFailed(error: String) {
                    LogUtils.d("v2开关请求结束  成功：false  耗时：${System.currentTimeMillis() - startTime}    $error")
                    trySend(false)
                }

                override fun onHandlerStart() = Unit
            }
        )

        awaitClose {
            LogUtils.d("v2SwitchKeyFlow: 获取v2开关流关闭")
        }
    }
}

class LauncherViewModel : ViewModel() {
    fun getTemplate() = viewModelScope.launch(Dispatchers.Default) {
        TemplateRepository.initTemplateData()
    }
}
